package com.maigao.entity.jpa;

import lombok.Data;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

@Data
@Entity
@Table(name = "tb_user")
@DynamicInsert
@DynamicUpdate
public class User implements Serializable {

    public enum UserType{
        NORMAL(0), ADMIN(1), SUPER_ADMIN(2);
        UserType(int value) {
            this.value = value;
        }
        private int value;

        public int getValue() {
            return value;
        }

        public static UserType getByValue(int value) {
            Optional<UserType> optional = Arrays.stream(values()).filter(p -> Objects.equals(p.getValue(), value)).findFirst();
            if (optional.isPresent()) {
                return optional.get();
            }
            return null;
        }
    }

    public enum UserStatus{
        NORMAL(0), DISABLED(-1);
        UserStatus(int value) {
            this.value = value;
        }
        private int value;

        public int getValue() {
            return value;
        }
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "uid")
    private String uid;

    @Column(name = "user_name")
    private String userName;

    @Column(name = "user_type")
    private Integer userType;

    @Column(name = "mobile")
    private String mobile;

    @Column(name = "user_status")
    private Integer userStatus;

    @Column(name = "wx_open_id")
    private String wxOpenId;

    @Column(name = "registration_code")
    private String registrationCode;

    @Column(name = "create_time")
    private LocalDateTime createTime;

    @Column(name = "last_login_time")
    private LocalDateTime lastLoginTime;

}
